import { Permission as PermissionEntity } from '@/entity/Permission';
export function createPermissionTreeByPath(
  permissionList: PermissionEntity[],
  directPath = '',
) {
  // 必须是菜单才有下一级
  const menuPermissionList = permissionList.filter(
    (permission) =>
      permission.funcType === '1' && permission.idPath === directPath,
  );
  // 只剩下功能按钮，直接全量返回给父级
  if (!menuPermissionList.length) {
    return permissionList.map((item) => ({ ...item, children: null }));
  }
  // 功能按钮不会丢失，会被重新传入 createPermissionTreeByPath
  return menuPermissionList.map((permission) => {
    let childrenPermission: PermissionEntity[] | null = null;
    const permissionDirectPath = `${permission.idPath}/${permission.id}`;
    // 所有的子数组
    childrenPermission = createPermissionTreeByPath(
      permissionList.filter((item) =>
        item.idPath.includes(permissionDirectPath),
      ),
      permissionDirectPath,
    );
    return {
      ...permission,
      children: childrenPermission,
    };
  });
}
